package org.smartopen.unionpay.openapi.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.smartframework.common.encrypt.RsaUtil;
import org.smartframework.common.encrypt.tuple.KeyPair;
import org.smartframework.core.api.Resp;
import org.smartframework.core.util.Charsets;
import org.smartframework.system.log.annotation.SysLog;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@Api(tags = {"开发工具"})
@RestController
@RequiredArgsConstructor
public class UnionPayOpenApiUtilController {


    @PostMapping("/sign")
    @ApiOperation("签名")
    @SysLog("签名")
    public Resp<String> sign(@RequestParam String priKey, @RequestParam String signContent) {
        return Resp.ok(RsaUtil.encryptByPrivateKeyToBase64(priKey, signContent.getBytes(Charsets.UTF_8)));
    }

    @PostMapping("/sign/generate")
    @ApiOperation("生成签名")
    @SysLog("生成签名")
    public Resp<HashMap> generate(@RequestParam(defaultValue = "1024") int keyszie) {
        final KeyPair keyPair = RsaUtil.genKeyPair(keyszie);
        String priKey = keyPair.getPrivateBase64();
        String pubKey = keyPair.getPublicBase64();
        return Resp.ok(new HashMap() {{
            put("privateKey", priKey);
            put("publicKey", pubKey);
        }});
    }
}
